/**
 * 车辆保养
*
*/
var express = require('express');
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var auth = require('../../auth/auth');
var util = require('../../util/commonutil');
var router = express.Router();
// var querystring = require('querystring');

router.get('/', function(req, res, next) {
   auth.checkAuth(req, res, next);
   res.render('baseCarKeep/index');
});
router.get('/list', function(req, res){
   var pathname = url.parse(req.url).pathname;  //pathname => list

   // var arg = url.parse(req.url).query;          //arg => name=a&id=5
   // console.log("Request for " + arg );
   // var str = qstr.parse(arg);                //str=> {name:'a',id:'5'}

   var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
   // console.log("Request for " + arg1 );

   // var name = qstr.parse(arg).draw;         //name => a
   // console.log("name = "+name);
   var query=req.query;//参数
   // req.params，req.query 和 req.body 中的表单数据
   request({
       url: pro.serveraddr+'/baseCarKeep/list',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data);
            }else {
                console.log("error= " + data.msg );
                res.json(data.msg);
            }



        }
    });
});
router.get('/edit', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCarKeep/edit',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.object);
            }else {
                console.log("error= " + data.msg );
                res.json(data.msg);
            }
        }
    });
});
router.get('/create', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCarKeep/create',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.obj);
            }else {
                console.log("error= " + data.msg );
                res.json(data.msg);
            }
        }
    });
});
router.post('/save', function(req, res){
    // req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr+'/baseCarKeep/save',
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        qs: req.body
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var info = body;
            res.json(info);
            // res.send();
        }else if (response.statusCode == 500) {
            res.send("服务错误!");
            console.log("error= 服务错误");
            // res.send();
        }else{
            console.log("error= " + error );
        }

    });
});
router.get('/del', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCarKeep/delete',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.msg);
            }else if (response.statusCode == 500) {
                res.send("服务错误!");
                console.log("error= 服务错误");
                // res.send();
            }else{
                console.log("error= " + error );
            }
        }
    });
});
module.exports = router;
